﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using xmlduqu.Models;

public static class XMLoperation
{
    /// <summary>
    /// XML 导入数据库
    /// </summary>
    /// <returns></returns>
    public static void XMLtoData()
    {
        string s = @"D:\property";
        var files = Directory.GetFiles(s, "*.xml");
        var xmlq = new List<bimProperties>();
        foreach (var fi in files)
        {
            var xm = new xmlqu();
            xm.name = fi;
            var xmldoc = new XmlDocument();
            xmldoc.Load(fi);
            XmlNode wal = xmldoc.SelectSingleNode("GraphicElement_Properties");
            XmlNodeList nodelist = wal.ChildNodes;
            foreach (var re in nodelist)
            {
                XmlElement xe = (XmlElement)re;
                XmlNodeList se = xe.ChildNodes;
                foreach (XmlNode xmlnode in se)
                {
                    var bimp = new bimProperties();
                    var f = fi.Split('_')[0];
                    var f1 = f.Split('\\');
                    bimp.userid = f1[f1.Length - 1];
                    var name = xmlnode.Name;
                    if (name == "ReferenceLine")
                    {
                        XmlElement xe1 = (XmlElement)xmlnode;
                        XmlNodeList se1 = xe1.ChildNodes;
                        XmlNodeList se2 = se1[0].ChildNodes;
                        foreach (XmlNode ite in se2)
                        {
                            XmlElement xex = (XmlElement)ite;
                            XmlNodeList se3 = xex.ChildNodes;
                            foreach (XmlNode it1 in se3)
                            {
                                if (it1.Name == "TransientLookupCollection")
                                {
                                    foreach (XmlNode item in it1.ChildNodes)
                                    {
                                        bimp.propertyname = item.Attributes["key"].Value;
                                        bimp.value = item.InnerText;
                                    }
                                }
                                else
                                {
                                    bimp.propertyname = it1.Name;
                                    bimp.value = it1.InnerText;
                                }

                            }
                        }
                    }
                    else
                    {
                        var c = name.Split('_');
                        if (c.Length < 4)
                        {
                            bimp.propertyname = c[0];
                        }
                        else
                        {
                            bimp.category = c[0];
                            bimp.propertyname = c[c.Length - 1];
                        }

                        var neir = xmlnode.InnerText;
                        bimp.value = neir;
                    }
                    var sql1 = @"select value from Dictionaries where name='" + bimp.propertyname + "'";
                    //var na = SelectSql(sql1);

                    //if (na != null)
                    //{
                    //    bimp.propertyname = na.ToString();
                    //}


                    //var sql = @"insert into bimProperties(userid,propertyname,category,value) values('" + bimp.userid + "','" + bimp.propertyname + "','" + bimp.category + "','" + bimp.value + "')";
                    //InsertSql(sql);
                    //xmlq.Add(bimp);
                }
            }

        }
    }
    /// <summary>
    /// 递归文件开始
    /// </summary>
    public static void wenjianKs()
    {
        string path = @"D:\x-s";
        DirectoryInfo fo = new DirectoryInfo(path);
        TOwenjian(fo);
    }
    /// <summary>
    /// 递归查看文件夹下的所有文件
    /// </summary>
    /// <param name="fo"></param>
    /// <returns></returns>
    public static void TOwenjian(DirectoryInfo fo)
    {
        ArrayList filelist = new ArrayList();
        FileInfo[] allfil = fo.GetFiles();
        var name = fo.Name;
        foreach (var fi in allfil)
        {
            //文件名称
            var userid = fi.Name.Split('.')[0];
            var guid = "";
            var bimp = "";
            var pitch = "";
            var zhanming = "";
            //文件地址
            var resPath = fi.FullName.Replace("D:\\x-s\\", "");
            var re = fi.DirectoryName.Replace("\\", "|").Split('|');
            var jd = "";
            var wd = "";
            var hei = "";
            var yaw = "";
            if (re[2] == "泰山路站")
            {
                zhanming = "琅琊新城";
                jd = "119.889004";
                wd = "35.67855";
                hei = "19.502568555";
                yaw = "292.";
            }
            else if (re[2] == "北京路站")
            {
                zhanming = "董家口";
                jd = "119.826898";
                wd = "35.680923";
                hei = "23.408365764";
                yaw = "126.";
            }
            for (int i = 3; i < re.Length; i++)
            {
                bimp += re[i] + "|";
            }
            if (bimp.Length > 0)
            {
                bimp = bimp.Substring(0, bimp.Length - 1);
            }


            var sql = @"insert into bimTarget(bimpath,bimproject,categoryID,loadOrder,name,properties,USerProperties,resPath,jd,wd,height,yaw,pitch,roll,userid,remark,scale,userdata,istm,minx,miny,minz,maxx,maxy,maxz,floorindex,worksetname,familyname,floorindex2,maxfloorIndex2,bimtype,relativeLocation) VALUES('" + bimp + "','" + zhanming + "','','1','" + userid + "','1','','" + resPath + "','" + jd + "','" + wd + "','" + hei + "','" + yaw + "','-90','0',ISNULL((SELECT MAX(userid) FROM bimTarget) + 1 , 1),'','1','','0','-9999','-9999','-9999','-9999','-9999','-9999','','','','1','1','0','') ";
            //InsertSql(sql);
            //,ISNULL((SELECT MAX(userid) FROM bimTarget) + 1 , 1),
        }
        DirectoryInfo[] alldir = fo.GetDirectories();
        foreach (var item in alldir)
        {
            TOwenjian(item);
        }
    }


}
